package com.boby.homecamera.webserver

import android.content.Context
import com.boby.homecamera.constant.Constant
import com.boby.lib_common.util.BLog
import com.yanzhenjie.andserver.AndServer
import com.yanzhenjie.andserver.Server
import java.util.concurrent.TimeUnit

class WebServerManager {
    private var mServer: Server? = null
    private val TAG = "WebServerManager"

    fun init(context:Context){
        mServer = AndServer.webServer(context)
            .port(Constant.HTTP_SERVER_PORT)
            .timeout(10, TimeUnit.SECONDS)
            .listener(object : Server.ServerListener {
                override fun onStarted() {
//                    val address: InetAddress = NetUtils.getLocalIPAddress()
//                    ServerManager.onServerStart(this@CoreService, address.hostAddress)
                    BLog.d(TAG, "onStarted")
                }

                override fun onStopped() {
                    mServer = null
                    BLog.d(TAG, "onStopped")
                }

                override fun onException(e: Exception) {
                    BLog.d(TAG, "onException")
                    e.printStackTrace()
                    mServer = null
                }
            })
            .build()
    }

    fun start(){
        mServer?.startup()
    }

    fun stop(){
        mServer?.shutdown()
        mServer = null
    }
}